package org.example.struct.array2;

import com.alibaba.fastjson.JSON;

/**
 * @author liyishan
 * @date 2024/9/11 10:46
 * @apiNote
 */

public class Test02653GetSubarrayBeauty {
    public static void main(String[] args) {
        int[] nums = {1,-1,-3,-2,-3};
        int k = 3;
        int x = 2;
        int[] res = getSubarrayBeauty(nums, k, x);
        System.out.println(JSON.toJSON(res));
    }
    public static int[] getSubarrayBeauty(int[] nums, int k, int x) {
        int n = nums.length;
        int[] heap =new int[101];
        int[] arr = new int[n-k+1];
        for(int i = 0;i<n;i++){
            heap[nums[i]+50]++;
            if(i >= k-1){
                int xth = smallXth(heap,x);
                arr[i-k+1] = Math.min(xth,0);
                heap[nums[i-k+1]+50]--;
            }
        }
        return arr;
    }
    public static int smallXth(int[] cnt,int x){
        int sum = 0;
        for(int i = 0;i<cnt.length;i++){
            sum += cnt[i];
            if(sum < x){
                continue;
            }
            return i-50;
        }
        return 0;
    }

}
